#ifndef _THREAD_HPP_
#define _THREAD_HPP_
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <functional>
#include <pthread.h>
#include <unistd.h>

namespace ThreadModule
{
    static u_int32_t number = 1; // bug?
    class Thread
    {
        using func_t = std::function<void()>; // 暂时这样写，完全够了
    private:
        void EnableDetach()
        {
            std::cout << "线程被分离了" << std::endl;
            _isdetach = true;
        }

        void EnableRunning()
        {
            _isrunning = true;
        }

        // 类内的成员函数默认包含this指针，这里其实是有两个参数，参数类型不兼容，所以设置为静态成员函数（属于类不再属于对象了）
        static void *Routine(void *args)
        {
            Thread *self = static_cast<Thread *>(args);
            self->EnableRunning();
            if (self->_isdetach)
                self->Detach();
            pthread_setname_np(self->_tid, self->_name.c_str()); // 设置线程的名字
            self->_func();                                       // // 回调处理。线程执行调出去的时候，该函数执行处理完还会回来
            return nullptr;
        }

    public:
        Thread(func_t func)
            : _tid(0),
              _isdetach(false),
              _isrunning(false),
              res(nullptr),
              _func(func)
        {
            _name = "thread-" + std::to_string(number++);
        }

        bool Start()
        {
            if (_isrunning)
                return false;
            int n = pthread_create(&_tid, nullptr, Routine, this); // 这里传this为了让Routine能够回调_func()
            if (n != 0)
            {
                std::cerr << "create thread error" << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << _name << " create success" << std::endl;
                return true;
            }
        }

        void Detach()
        {
            if (_isdetach)
                return;
            if (_isrunning)
                pthread_detach(_tid);
            EnableDetach(); // 线程启动前或者启动后分离，标志位都要置为1。
        }

        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cerr << "cancel thread error" << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    _isrunning = false;
                    std::cout << _name << " stop" << std::endl;
                    return true;
                }
            }
            return false;
        }

        void Join()
        {
            if (_isdetach)
            {
                std::cout << "你的线程已经是分离的了,不能进行join" << std::endl;
                return;
            }
            int n = pthread_join(_tid, &res);
            if (n != 0)
            {
                std::cerr << "join thread error: " << strerror(n) << std::endl;
            }
            else
            {
                std::cout << "join thread success" << std::endl;
            }
        }

        ~Thread() {}

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isrunning;
        void *res;
        func_t _func;
    };
} // namespace ThreadModule

#endif
